package xyz.heyaoshare.core.controller.auth;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import org.springframework.web.bind.annotation.*;
import xyz.heyaoshare.annotation.OperationLog;
import xyz.heyaoshare.common.entity.bo.Result;
import xyz.heyaoshare.core.controller.auth.vo.req.UserLoginReqVO;
import xyz.heyaoshare.core.controller.auth.vo.req.ValidateCaptchaReqVO;
import xyz.heyaoshare.core.controller.auth.vo.resp.CaptchaRespVO;
import xyz.heyaoshare.core.controller.auth.vo.resp.UserInfoRespVO;
import xyz.heyaoshare.core.controller.auth.vo.resp.UserLoginRespVO;
import xyz.heyaoshare.core.service.auth.AuthService;
import xyz.heyaoshare.core.service.common.CaptchaService;

import static xyz.heyaoshare.common.constants.VersionManager.V001;

/**
 * 认证中心
 * @author YueHe
 * @version 0.0.1
 * @since 2025/6/25 下午2:41
 */
@RestController
@RequestMapping(V001 + "/auth")
@Tag(name = "认证中心")
public class AuthController {

    @Resource
    private CaptchaService captchaService;
    @Resource
    private AuthService authService;

    @OperationLog
    @GetMapping("/get/captcha")
    @Operation(summary = "获取验证码")
    public Result<CaptchaRespVO> getCaptcha() {
        return Result.ok(captchaService.getCaptcha());
    }

    @OperationLog
    @PostMapping("/validate/captcha")
    @Operation(summary = "校验验证码")
    public Result<String> validateCaptcha(@RequestBody @Valid ValidateCaptchaReqVO reqVO) {
        captchaService.validateCaptcha(reqVO.getKey(), reqVO.getUserCode());
        return Result.ok("验证码校验成功");
    }

    @OperationLog
    @PostMapping("/login")
    @Operation(summary = "用户登录")
    public Result<UserLoginRespVO> login(@RequestBody @Valid UserLoginReqVO reqVO) {
        return Result.ok(authService.login(reqVO));
    }

    @OperationLog
    @GetMapping("/userinfo")
    @Operation(summary = "获取用户信息")
    public Result<UserInfoRespVO> getUserinfo() {
        return Result.ok(authService.getUserinfo());
    }

    @OperationLog
    @PostMapping("/logout")
    @Operation(summary = "用户注销")
    public Result<String> logout() {
        authService.logout();
        return Result.ok("注销成功");
    }

}